package com.per.mybootall.test;

import lombok.SneakyThrows;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/1/19
 * Time: 14:53
 * Description:volatile测试【多线程中该值被变更后会被其他线程立即知晓】
 */
public class test10 {

    private static volatile boolean is_Ready;

    public static void main(String[] args) throws InterruptedException {
        is_Ready = false;
        printMsg(1);
      //  printMsg2(2);

        Thread.sleep(2000);
        is_Ready = true;
       // printMsg(2);
        printMsg2(2);
    }


    public static void printMsg(int i) {
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i=0;i<10;i++){
                    Thread.sleep(1000);
                    if (is_Ready) {
                        System.out.println(i+"准备好了"+is_Ready);
                    } else {
                        System.out.println(i+"没准备好"+is_Ready);
                    }
                }
            }
        }).start();

    }

    public static void printMsg2(int i) {
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i=0;i<10;i++){
                    Thread.sleep(1000);
                    if (is_Ready) {
                        System.out.println(i+"222好了"+is_Ready);
                    } else {
                        System.out.println(i+"222没好"+is_Ready);
                    }
                }
            }
        }).start();

    }
//    public static void printMsg3(int i) {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                if (is_Ready) {
//                    System.out.println(i+"333准备好了"+is_Ready);
//                } else {
//                    System.out.println(i+"333没准备好"+is_Ready);
//                }
//            }
//        }).start();
//
//    }
}
